package com.yichen.bean.processor;

import com.yichen.bean.pojo.People;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @Classname MyInstantiationAwareBeanPostProcessor
 * @Description
 * @Author caozhenhao001
 * @Date 2024/3/28
 * @Version
 */
//@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if(bean instanceof People) {
            People people = (People) bean;
            people.setName("zhang san");
            people.setAge(26);
            people.setSex("woman");
            System.out.println("----postProcessProperties");
        }
        return pvs;
    }

    /**
     * BeanPostProcessor接口中的方法
	 * 在Bean的自定义初始化方法之前执行
	 * Bean对象已经存在了
	 */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if("people".equals(beanName)) {
            System.out.println(">>postProcessBeforeInitialization");
            return bean;
        }
        return bean;
    }

    /**
     * BeanPostProcessor接口中的方法
     * 在Bean的自定义初始化方法执行完成之后执行
     * Bean对象已经存在了
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if("people".equals(beanName)) {
            System.out.println("<<postProcessAfterInitialization");
            return bean;
        }
        return bean;
    }

    /**
     * InstantiationAwareBeanPostProcessor中自定义的方法
     * 在方法实例化之前执行  Bean对象还没有
     */
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if("people".equals(beanName)) {
            System.out.println("--->postProcessBeforeInstantiation");
        }

        return null;
    }

    /**
     * InstantiationAwareBeanPostProcessor中自定义的方法
     * 在方法实例化之后执行  Bean对象已经创建出来了
     */
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if("people".equals(beanName)) {
            System.out.println("<---postProcessAfterInstantiation");
            return true;
        }
        return true;
    }


}
